﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using StoreManager.Models;
using StoreManager.DAO;

namespace StoreManager.Controllers
{
    public class ImportController : Controller
    {
        private StoreManagerContext db = new StoreManagerContext();

        //
        // GET: /Import/

        public ActionResult Index()
        {
            ImportOrderSearchView importOrderSearchView = new ImportOrderSearchView();            
            return View(importOrderSearchView);
        }
    
        [HttpPost]
        public ActionResult Index(ImportOrderSearchView order)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    order.SearchResults = OrderDAO.SearchImportOrders(order);
                }
                return View(order);
            }
            catch (Exception ex)
            {
                System.Console.WriteLine("ImportController:" + ex.Message);
                //Redirect to Error page
                return RedirectToAction("Error", "Home");
            }
        }
        //
        // GET: /Import/Details/5

        public ActionResult Details(Guid id)
        {
            ImportOrder importorder = db.ImportOrders.Find(id);
            if (importorder == null)
            {
                return HttpNotFound();
            }           
            return View(importorder);
        }

        //
        // GET: /Import/Create

        public ActionResult Create()
        {
            ImportOrder order = new ImportOrder();

            var detailList = new List<ImportOrderDetail>();
            ImportOrderDetail d = new ImportOrderDetail();
            detailList.Add(d);
            order.ImportOrderDetails = detailList;
            //Set a list for display dropdown list in screen
            //Param 1- ObjectList
            //Param 2- attribute Name for option value
            //Param 3- attribute Name for option label
            //param 4- selected value
            var products = db.Products.OrderBy(p => p.Code);
            ViewBag.ProductID = new SelectList(products.ToList(), "Code", "Name", "");
            var importTypes = db.SMCodes.Where(s => s.GroupCode.Contains(SMGroupCodes.IMPORT_TRANS)).OrderBy(s => s.Code);         
            ViewBag.Type = new SelectList(importTypes.ToList(), "Code", "Name", "");
            var proTypes = db.SMCodes.Where(s => s.GroupCode.Contains(SMGroupCodes.PRODUCT_TYPES)).OrderBy(s => s.Code);

            var staffs = db.Staffs.Where(s => s.Status.Contains(SMCodes.STAFF_STATUS_CODE_VALID)).OrderBy(s => s.Name);
            ViewBag.Staff = new SelectList(staffs.ToList(), "StaffID", "Name", "");

            var supplier = db.Suppliers.Where(su => su.Status.Contains(SMCodes.SUPL_STATUS_CODE_VALID)).OrderBy(su => su.Name);
            ViewBag.Supplier = new SelectList(supplier.ToList(), "SupplierID", "Name", "");

            ViewBag.ProductType = new SelectList(proTypes.ToList(), "Code", "Name", "");
            return View(order);
        }

        public ActionResult Add(ImportOrder importorder)
        {

            if (importorder.ImportOrderDetails == null)
            {
                importorder.ImportOrderDetails = new List<ImportOrderDetail>();
            }
            var detailList = importorder.ImportOrderDetails;
            ImportOrderDetail d = new ImportOrderDetail();
            detailList.Add(d);
            importorder.ImportOrderDetails = detailList;
            //Set a list for display dropdown list in screen
            //Param 1- ObjectList
            //Param 2- attribute Name for option value
            //Param 3- attribute Name for option label
            //param 4- selected value
            var products = db.Products.OrderBy(p => p.Code);
            ViewBag.ProductID = new SelectList(products.ToList(), "Code", "Name", "");
            var importTypes = db.SMCodes.Where(s => s.GroupCode.Contains(SMGroupCodes.IMPORT_TRANS)).OrderBy(s => s.Code);
            ViewBag.Type = new SelectList(importTypes.ToList(), "Code", "Name", "");
            var proTypes = db.SMCodes.Where(s => s.GroupCode.Contains(SMGroupCodes.PRODUCT_TYPES)).OrderBy(s => s.Code);
            ViewBag.ProductType = new SelectList(proTypes.ToList(), "Code", "Name", "");
            return View(importorder);
        }
        //
        // POST: /Import/Create

        [HttpPost]
        public ActionResult Create(ImportOrder importorder)
        {
            if (ModelState.IsValid)
            {
                importorder.ImportOrderID = Guid.NewGuid();
                db.ImportOrders.Add(importorder);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            //Set a list for display dropdown list in screen
            //Param 1- ObjectList
            //Param 2- attribute Name for option value
            //Param 3- attribute Name for option label
            //param 4- selected value
            var staffs = db.Staffs.Where(s => s.Status.Contains(SMCodes.STAFF_STATUS_CODE_VALID)).OrderBy(s => s.Name);
            ViewBag.Staff = new SelectList(staffs.ToList(), "StaffID", "Name", importorder.StaffID);

            var supplier = db.Suppliers.Where(su => su.Status.Contains(SMCodes.SUPL_STATUS_CODE_VALID)).OrderBy(su => su.Name);
            ViewBag.Supplier = new SelectList(supplier.ToList(), "SupplierID", "Name", importorder.SupplierID);

            var products = db.Products.OrderBy(p => p.Code);
            ViewBag.ProductID = new SelectList(products.ToList(), "Code", "Name", "");
            var importTypes = db.SMCodes.Where(s => s.GroupCode.Contains(SMGroupCodes.IMPORT_TRANS)).OrderBy(s => s.Code);
            ViewBag.Type = new SelectList(importTypes.ToList(), "Code", "Name", importorder.Type);
            var proTypes = db.SMCodes.Where(s => s.GroupCode.Contains(SMGroupCodes.PRODUCT_TYPES)).OrderBy(s => s.Code);
            ViewBag.ProductType = new SelectList(proTypes.ToList(), "Code", "Name", "");
            return View(importorder);
        }

        //
        // GET: /Import/Edit/5

        public ActionResult Edit(Guid id)
        {
            ImportOrder importorder = db.ImportOrders.Find(id);
            if (importorder == null)
            {
                return HttpNotFound();
            }
            importorder.ImportOrderDetails =
                db.ImportOrderDetails.Where(i=> i.ImportOrderID.Equals(importorder.ImportOrderID)).ToList();

            var staffs = db.Staffs.Where(s => s.Status.Contains(SMCodes.STAFF_STATUS_CODE_VALID)).OrderBy(s => s.Name);
            ViewBag.Staff = new SelectList(staffs.ToList(), "StaffID", "Name", importorder.StaffID);

            var supplier = db.Suppliers.Where(su => su.Status.Contains(SMCodes.SUPL_STATUS_CODE_VALID)).OrderBy(su => su.Name);
            ViewBag.Supplier = new SelectList(supplier.ToList(), "SupplierID", "Name", importorder.SupplierID);

            var products = db.Products.OrderBy(p => p.Code);
            ViewBag.ProductID = new SelectList(products.ToList(), "Code", "Name", "");
            var importTypes = db.SMCodes.Where(s => s.GroupCode.Contains(SMGroupCodes.IMPORT_TRANS)).OrderBy(s => s.Code);
            ViewBag.Type = new SelectList(importTypes.ToList(), "Code", "Name", importorder.Type);
            var proTypes = db.SMCodes.Where(s => s.GroupCode.Contains(SMGroupCodes.PRODUCT_TYPES)).OrderBy(s => s.Code);
            ViewBag.ProductType = new SelectList(proTypes.ToList(), "Code", "Name", "");
            return View(importorder);
        }

        //
        // POST: /Import/Edit/5

        [HttpPost]
        public ActionResult Edit(ImportOrder importorder)
        {
            if (ModelState.IsValid)
            {
                System.Console.Out.Write("ImportOrderID=" + importorder.ImportOrderID);                
                db.Entry(importorder).State = EntityState.Modified;
                var list = db.ImportOrderDetails.Where(im => im.ImportOrderID.Equals(importorder.ImportOrderID));
                foreach (var oldItem in list)
                {
                    foreach(var uItem in importorder.ImportOrderDetails) {

                        if (oldItem.ImportOrderDetailsID.Equals(uItem.ImportOrderDetailsID))
                        {
                           uItem.Created = oldItem.Created;
                           uItem.CreatedBy = oldItem.CreatedBy;
                           uItem.LastModified = oldItem.LastModified;
                            // if(!uItem.LastModified.Equals(oldItem.LastModified)) {
                                //return View(importorder);
                            //}
                        }
                    }
                }
                System.Console.Out.Write("");
                db.Entry(importorder).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            var staffs = db.Staffs.Where(s => s.Status.Contains(SMCodes.STAFF_STATUS_CODE_VALID)).OrderBy(s => s.Name);
            ViewBag.Staff = new SelectList(staffs.ToList(), "StaffID", "Name", importorder.StaffID);

            var supplier = db.Suppliers.Where(su => su.Status.Contains(SMCodes.SUPL_STATUS_CODE_VALID)).OrderBy(su => su.Name);
            ViewBag.Supplier = new SelectList(supplier.ToList(), "SupplierID", "Name", importorder.SupplierID);

            var products = db.Products.OrderBy(p => p.Code);
            ViewBag.ProductID = new SelectList(products.ToList(), "Code", "Name", "");
            var importTypes = db.SMCodes.Where(s => s.GroupCode.Contains(SMGroupCodes.IMPORT_TRANS)).OrderBy(s => s.Code);
            ViewBag.Type = new SelectList(importTypes.ToList(), "Code", "Name", importorder.Type);
            var proTypes = db.SMCodes.Where(s => s.GroupCode.Contains(SMGroupCodes.PRODUCT_TYPES)).OrderBy(s => s.Code);
            ViewBag.ProductType = new SelectList(proTypes.ToList(), "Code", "Name", "");
            return View(importorder);
        }

        //
        // GET: /Import/Delete/5

        public ActionResult Delete(Guid id)
        {
            ImportOrder importorder = db.ImportOrders.Find(id);
            if (importorder == null)
            {
                return HttpNotFound();
            }
            return View(importorder);
        }

        //
        // POST: /Import/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(Guid id)
        {
            ImportOrder importorder = db.ImportOrders.Find(id);
            db.ImportOrders.Remove(importorder);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}